package stc.skymobi.netty.handler.codec.memcached.bean.response;

import stc.skymobi.bean.bytebean.annotation.ByteField;
import stc.skymobi.netty.handler.codec.memcached.bean.core.BinaryCommonHeader;

import java.util.HashMap;
import java.util.Map;

public class BinaryResponseHeader extends BinaryCommonHeader {
    public static Map<Integer,String> RESPONSE_STATUS = new HashMap<Integer,String>();

    /**
     * Status of the response (non-zero on error), Response header only
     */
    @ByteField(index = 5,description = "Status of the response (non-zero on error), Response header only")
    private short status;

    public BinaryResponseHeader(){
        this.setMagic(MAGIC_RESP);
    }

    public short getStatus() {
        return status;
    }

    public String getStatusString() {
        return RESPONSE_STATUS.get((int)status);
    }

    public void setStatus(short status) {
        this.status = status;
    }

    public boolean isSuccess(){
        return this.status == 0;
    }

    @Override
    public String toString() {
        return "BinaryResponseHeader{" +
                "status=" + status +"("+getStatusString()+
                ")} " + super.toString();
    }

static {
        RESPONSE_STATUS.put(0x0000,"No error");
        RESPONSE_STATUS.put(0x0001,"Key not found");
        RESPONSE_STATUS.put(0x0002,"Key exists");
        RESPONSE_STATUS.put(0x0003,"Value too large");
        RESPONSE_STATUS.put(0x0004,"Invalid arguments");
        RESPONSE_STATUS.put(0x0005,"Item not stored");
        RESPONSE_STATUS.put(0x0006,"Incr/Decr on non-numeric value");
        RESPONSE_STATUS.put(0x0007,"The vbucket belongs to another server");
        RESPONSE_STATUS.put(0x0008,"Authentication error");
        RESPONSE_STATUS.put(0x0009,"Authentication continue");
        RESPONSE_STATUS.put(0x0081,"Unknown command");
        RESPONSE_STATUS.put(0x0082,"Out of memory");
        RESPONSE_STATUS.put(0x0083,"Not supported");
        RESPONSE_STATUS.put(0x0084,"Internal error");
        RESPONSE_STATUS.put(0x0085,"Busy");
        RESPONSE_STATUS.put(0x0086,"Temporary failure");
    }

}
